package a06_栈与队列;

import java.util.Stack;

/**
 * <p>
 * a01_用栈实现队列
 * </p>
 *
 * @author flyduck
 * @since 2024-07-25
 */
public class a02_用栈实现队列 {

    public static void main(String[] args) {
        Stack<Integer> stack1 = new Stack<>();
        stack1.add(1);
        stack1.add(2);
        System.out.println(stack1.peek());
    }

}

class MyQueue1 {

    private Stack<Integer> inStack;
    private Stack<Integer> outStack;

    public MyQueue1() {
        inStack = new Stack<>();
        outStack = new Stack<>();
    }

    public void push(int x) {
        inStack.push(x);
    }

    public int pop() {
        if(!outStack.isEmpty()){
            return outStack.pop();
        }else {
            while (!inStack.isEmpty()){
                outStack.push(inStack.pop());
            }
            return outStack.pop();
        }
    }

    public int peek() {
        if(!outStack.isEmpty()){
            return outStack.peek();
        }else {
            while (!inStack.isEmpty()){
                outStack.push(inStack.pop());
            }
            return outStack.peek();
        }
    }

    public boolean empty() {
        return inStack.empty() && outStack.empty();
    }
}
